<?php
namespace Home\Controller;
use Think\Controller;
class FlowerModelController extends Controller {

	private $m = '';
	public function _initialize(){
		if($m == ''){
			$this->m = M('Flower');
		}
	}


	//更新点赞状态
	public function update_flower($openid,$aid){
		$data = $this->m->where(array('flower_uid'=>$openid))->find();
		if($data){
			$aids = json_decode($data['flower_aids'],1);
			if(array_search($aid,$aids) !== false){	//如果有
				$index = array_search($aid,$aids);
				array_splice($aids,$index,1);
				$mode = 'reduce';
			}else{
				array_push($aids,$aid);
				$mode = 'add';
			}
			$st = $this->m->where(array('flower_uid'=>$openid))->save(array('flower_aids'=>json_encode($aids)));
			if($st){
				$cls_Album = new AlbumModelController();
				$field = 'album_id,album_flower';
				$data = $cls_Album->get_album_info($aid);
				if(!$data){
					exit;
				}
				if($mode == 'reduce'){
					$data['album_flower'] = $data['album_flower'] - 1; 
				}else{
					$data['album_flower'] = $data['album_flower'] + 1; 
				}
				$cls_Album->album_edits($data,1);
				return json_encode($aids);
			}else{
				return 'e11';
			}
		}else{
			$arr[] = $aid;
			$aids = json_encode($arr);
			$data['flower_aids'] = $aids;
			$data['flower_uid'] = $openid;
			if($this->m->create($data)){
				$st = $this->m->add();
				if($st){
					return $adis;
				}else{
					return 'e2';
				}
			}
		}
	}

	public function get_flower_all($openid){
		if(!$openid){
			exit;
		}
		$data = $this->m->where(array('flower_uid'=>$openid))->find();
		if($data){
			return $data;
		}
	}
}
